// pages/productmanage/productmanage.js
import { getStockCategoy} from '../../../utils/networkRequest.js'
import { errorMsg } from '../../../utils/tool.js'
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productItems: [], //商品分类列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    getStockCategoy("",(r)=>{ 
      if(r.code==0){
        self.setData({
          productItems:r.data
        })
      }else{
        errorMsg(r.message)
      } 
    })
  },
  //重新获取数据源
  onShow:function(){
    let addProductInfo = app.globalData.addProductInfo
    if (addProductInfo){
      this.data.productItems.map((o,i)=>{
        if (addProductInfo.cat_id == o.id){
          o.count = addProductInfo.cat_count
          o.children.map((oo,ii)=>{
            if (oo.id == addProductInfo.service_id){
              oo.count = addProductInfo.stock_count;
            }
          })
        } 
      })
    }
    this.setData({
      productItems: this.data.productItems
    })
    app.globalData.addProductInfo = null;
  },
  //点击项目
  onProductItem: function (e) {
    let item = e.currentTarget.dataset.productitem;
    this.data.productItems.map((o, i) => {
      if (o.id == item.id) {
        o.isActive = !o.isActive
      }
    })

    this.setData({
      productItems: this.data.productItems
    })

  },
//点击子类
  onChildItem: function (e) {
   let item =   e.currentTarget.dataset.childitem; 
   let classindex = e.currentTarget.dataset.classindex; 
   getStockCategoy(item.id,(r)=>{
     wx.navigateTo({
       url: '../productList/productList?productList=' + JSON.stringify(r.data) + "&item=" + JSON.stringify(item) + "&classindex=" + classindex
     })
   })

  },
  //添加项目
  goAddProduct: function () {
    wx.navigateTo({
      url: '../addproduct/addproduct',
    })
  }
  
})